package Driver;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Login {

	private WebDriver driver;
	
	public Login(WebDriver d){
		this.driver = d;
	}
	
	public String logar(String username, String password){
		String mensagem = null;
		
		driver.get("http://localhost/login.php");

		//Esperar 2 segundos para carregar a pagina
		WebDriverWait wait = new WebDriverWait(driver, 2);
		
		WebElement campoUserName = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("username")));
		campoUserName.sendKeys(username);
		
		WebElement campoPassword = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("password")));
		campoPassword.sendKeys(password);
		
		
		driver.findElement(By.id("btn-submit")).click();

		//Capturando o alert do sistema
		Alert alert = driver.switchTo().alert();
		
		mensagem = alert.getText();
		
		alert.accept();
		
		driver.close();
		return mensagem;
	}
}
